package com.joinval.modules.sys.controller;

import com.joinval.common.utils.PageUtils;
import com.joinval.common.utils.Result;
import com.joinval.modules.sys.entity.SysRoleEntity;
import com.joinval.modules.sys.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 角色管理
 *
 */
@Api(description = "角色控制器")
@RestController
@RequestMapping("/sys/role")
public class SysRoleController extends AbstractController {
	@Autowired
	private SysRoleService sysRoleService;

	@ApiOperation(value="新增角色")
	@PostMapping("/add")
	public Result add(@RequestBody @Valid SysRoleEntity role){
		sysRoleService.saveRole(role);
		return Result.ok();
	}

	@ApiOperation(value="修改角色")
	@PostMapping("/update")
	public Result update(@RequestBody @Valid SysRoleEntity role){
		sysRoleService.update(role);
		return Result.ok();
	}


	@ApiOperation(value="查询角色")
	@GetMapping("/list")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "pageNo", value = "分页启始值", required = false, paramType = "query", dataType = "Integer"),
			@ApiImplicitParam(name = "pageSize", value = "分页大小", required = false, paramType = "query", dataType = "Integer"),
			@ApiImplicitParam(name = "sortField", value = "排序字段", required = false, paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "sortOrder", value = "排序规则 asc:正排 desc:倒排", required = false, paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "roleName", value = "角色名", required = false, paramType = "query", dataType = "String")
	})
	public Result list(@RequestParam(value = "pageNo",required = false,defaultValue ="0") Integer pageNo,
						   @RequestParam(value = "pageSize",required = false,defaultValue ="10") Integer pageSize,
						   @RequestParam(value = "sortField",required = false,defaultValue ="createTime") String sortField,
						   @RequestParam(value = "sortOrder",required = false,defaultValue ="desc") String sortOrder,
						   @RequestParam(value = "roleName",required = false) String roleName
	){
		 PageUtils page = sysRoleService.list(PageUtils.parsePageInfo(pageNo, pageSize, sortField, sortOrder),roleName);
		 return Result.ok().data(page);
	}

	@ApiOperation(value="删除角色")
	@PostMapping("/delete")
	public Result delete(@RequestBody Long[] roleIds){
		sysRoleService.deleteBatch(roleIds);
		return Result.ok();
	}

	@ApiOperation(value="角色详情")
	@GetMapping("/info")
	public Result info(@RequestParam("roleId") Long roleId){
		Map<String, Object> info = sysRoleService.info(roleId);
		return Result.ok().data(info);
	}

	@ApiOperation(value="角色下拉菜单")
	@GetMapping("/menu")
	public Result menu(){
		List<Map<String, Object>> info = sysRoleService.menu();
		return Result.ok().data(info);
	}



	/**
	 * 角色列表
	 *//*
	@GetMapping("/list")
	@RequiresPermissions("sys:role:list")
	public R list(@RequestParam Map<String, Object> params){
		//如果不是超级管理员，则只查询自己创建的角色列表
		if(getUserId() != Constant.SUPER_ADMIN){
			params.put("createUserId", getUserId());
		}

		PageUtils page = sysRoleService.queryPage(params);

		return R.ok().put("page", page);
	}

	*//**
	 * 角色列表
	 *//*
	@GetMapping("/select")
	@RequiresPermissions("sys:role:select")
	public R select(){
		Map<String, Object> map = new HashMap<>();

		//如果不是超级管理员，则只查询自己所拥有的角色列表
		if(getUserId() != Constant.SUPER_ADMIN){
			map.put("create_user_id", getUserId());
		}
		List<SysRoleEntity> list = (List<SysRoleEntity>) sysRoleService.listByMap(map);

		return R.ok().put("list", list);
	}

	*//**
	 * 角色信息
	 *//*
	@GetMapping("/info/{roleId}")
	@RequiresPermissions("sys:role:info")
	public R info(@PathVariable("roleId") Long roleId){
		SysRoleEntity role = sysRoleService.getById(roleId);

		//查询角色对应的菜单
		List<Long> menuIdList = sysRoleMenuService.queryMenuIdList(roleId);
		role.setMenuIdList(menuIdList);

		return R.ok().put("role", role);
	}

	*//**
	 * 保存角色
	 *//*
	@SysLog("保存角色")
	@PostMapping("/save")
	@RequiresPermissions("sys:role:save")
	public R save(@RequestBody SysRoleEntity role){
		ValidatorUtils.validateEntity(role);

		role.setCreateUserId(getUserId());
		sysRoleService.saveRole(role);

		return R.ok();
	}

	*//**
	 * 修改角色
	 *//*
	@SysLog("修改角色")
	@PostMapping("/update")
	@RequiresPermissions("sys:role:update")
	public R update(@RequestBody SysRoleEntity role){
		ValidatorUtils.validateEntity(role);

		role.setCreateUserId(getUserId());
		sysRoleService.update(role);

		return R.ok();
	}

	*//**
	 * 删除角色
	 *//*
	@SysLog("删除角色")
	@PostMapping("/delete")
	@RequiresPermissions("sys:role:delete")
	public R delete(@RequestBody Long[] roleIds){
		sysRoleService.deleteBatch(roleIds);

		return R.ok();
	}*/
}
